

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:huajianshe_app/base/AppNavigator.dart';
import 'package:huajianshe_app/common/AppRouter.dart';
import 'package:huajianshe_app/common/load_image.dart';
import 'package:huajianshe_app/common/space.dart';
import 'package:huajianshe_app/widget/RoundedCornerWidget.dart';

class AutoGridWidgetV2 extends StatelessWidget {

  final int rowNum;
  final int colNum;
  final Widget? moreView;
  final double padding;
  final List<Widget> widgets;
  final double radius;
  final double? height;
  final Function(BuildContext context, int index)? onclick;

  const AutoGridWidgetV2({
    Key? key,
    this.rowNum = 1,
    this.colNum = 2,
    this.moreView,
    this.padding = 3,
    this.widgets = const [],
    this.radius = 0,
    this.height,
    this.onclick
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    int num = moreView == null ? colNum : colNum - 1;
    List<Widget> widgetList = widgets.toList();
    if (num < widgets.length) {
      widgetList = widgets.sublist(0, num);
    }
    if (moreView != null) widgetList.add(moreView!);
    for (int i = widgetList.length; i < colNum; i++) {
      widgetList.add(W(0));
    }
    List<Widget> result = [];
    for (int i = 0; i < num; i++) {
      if ((height ?? 0) >= 1) {
        Widget child = RoundedCornerWidget(height: height, width: double.infinity, borderColor: Colors.transparent,
            borderWidth: 0, radius: radius, child: widgetList[i]);
        if (onclick != null) {
          child = GestureDetector(onTap: () { if (onclick != null) onclick!(context, i); }, child: child);
        }
        result.add(Flexible(
            child: child,
        ));
      } else {
        Widget child = RoundedCornerWidget(borderColor: Colors.transparent, borderWidth: 0,
            radius: radius, child: widgetList[i]);
        if (onclick != null) {
          child = GestureDetector(onTap: () { if (onclick != null) onclick!(context, i); }, child: child);
        }
        result.add(Flexible(
            child: AspectRatio(aspectRatio: 1, child: child)
        ));
      }
      if (i != num - 1) {
        result.add(W((padding)));
      }
    }
    return Row(
      children: [
        ...result
      ],
    );
    
    // return LayoutBuilder(
    //     builder: (BuildContext context, BoxConstraints constraints) {
    //       print('constraints.maxWidth  ${constraints.maxWidth} height ${constraints.maxHeight}');
    //       double width = constraints.maxWidth;
    //       double gridSize = (width - (colNum - 1) * padding) / colNum;
    //       int num = moreView == null ? colNum : colNum - 1;
    //       List<Widget> widgetList = widgets;
    //       if (num < widgets.length) {
    //         widgetList = widgets.sublist(0, num);
    //       }
    //       if (moreView != null) widgetList.add(moreView!);
    //       for (int i = widgetList.length; i < colNum; i++) {
    //         widgetList.add(W(0));
    //       }
    //       List<Widget> result = widgetList.map((e) {
    //         // if (e == null) return SizedBox(width: gridSize, height: gridSize, child: Text(''),);
    //         return RoundedCornerWidget(borderColor: Colors.transparent, borderWidth: 0, radius: radius, child: e, width: gridSize, height: gridSize,);
    //       }).toList();
    //
    //       return Row(mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
    //         ...result
    //       ]);
    //     });
    }

    // viewImage(BuildContext context, int i) {
    //     // AppNavigator.push(context, RouteName.IMAGE_VIEWER, arguments: {'urls': })
    // }

}

class AutoGridImageV2 extends StatelessWidget {
  final int rowNum;
  final int colNum;
  final Widget? moreView;
  final double padding;
  final List<String?> imageUrls;
  final double radius;

  const AutoGridImageV2({
    Key? key,
    this.rowNum = 1,
    this.colNum = 2,
    this.moreView,
    this.padding = 3,
    this.imageUrls = const [],
    this.radius = 0
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    List<Widget> list = imageUrls.map((e) {
      if (e?.isNotEmpty == true) {
        return LoadImage(e!);
      } else {
        return W(0);
      }
    }).toList();
    return AutoGridWidgetV2(rowNum: rowNum, colNum: colNum, moreView: moreView, padding: padding, widgets: list, radius: radius,
      onclick: viewImage);
  }

  viewImage(BuildContext context, int index) {
    AppNavigator.push(context, RouteName.IMAGE_VIEWER, arguments: {'urls': imageUrls, 'index': index});
  }
}